<template>
  <div class="com_container">
    <div class="com_chart" ref="ref_seller"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      echartInstance: null,
      allData: [],
      currentPage: 2,
      totalPage: 0,
      // 标识定时器
      tiemid: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      console.log('主题改变了')
      console.log(this.theme)

      this.echartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.upData() // 更新图表的展示
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    // 屏幕改变事件事件监听
    window.addEventListener('resize', this.screenAdapter)
    // 调用screenAdapter
    this.screenAdapter()
  },
  destroyed() {
    clearInterval(this.tiemid)
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化echartInstance 对象
    // ef 加在普通的元素上，用this.$refs.（ref值） 获取到的是dom元素
    initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.ref_seller, this.theme)
      const initOption = {
        title: {
          text: '▎商家销售统计',
          left: 20,
          top: 20
        },
        grid: {
          top: '20%',
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0,
            lineStyle: {
              color: '#2D3443'
            }
          }
        },
        series: [
          {
            type: 'bar',

            label: {
              show: true,
              position: 'right',
              textStyle: {
                color: 'white'
              }
            },
            itemStyle: {
              // 指明颜色渐变的方向
              // 指明不同百分比之下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#5052EE' },
                { offset: 0.5, color: '#44C0C1' },
                { offset: 1, color: '#71C8B1' }
              ])
            }
          }
        ]
      }
      this.echartInstance.setOption(initOption)
      // console.log(this.$refs.ref_seller)
      // 对鼠标移出事件的监听
      this.echartInstance.on('mouseover', () => {
        clearInterval(this.tiemid)
      })
      this.echartInstance.on('mouseout', () => {
        this.openSetInterval()
      })
    },
    //    获取配置数据
    async getData() {
      const { data: res } = await this.$http.get('seller')
      console.log(res)
      this.allData = res
      // 将数据从小到大排序
      this.allData.sort((a, b) => {
        // a.value - b.value     b.value - a.value 则是从大到小
        return a.value - b.value
      })
      // 获得数据的总页数
      this.totalPage = this.allData.length % 5 === 0 ? this.allData.length / 5 : this.allData.length / 5 + 1
      this.upData()
      this.openSetInterval()
    },
    // 更新图表
    upData() {
      // 將每页的数据展示到当前页
      // 将数据截取从start 到 end
      const start = (this.currentPage - 1) * 5
      const end = this.currentPage * 5
      const showData = this.allData.slice(start, end)
      // 將echart 配置数据 筛选出来
      const sellerName = showData.map((i) => i.name)
      // console.log(sellerName)
      const sellerValue = showData.map((i) => i.value)
      // console.log(sellerValue)

      const dataOption = {
        yAxis: {
          data: sellerName
        },
        series: [
          {
            data: sellerValue
          }
        ]
      }

      // 使用数据显示图表。
      this.echartInstance.setOption(dataOption)
      // console.log(option)
    },
    openSetInterval() {
      if (this.tiemid) {
        clearInterval(this.tiemid)
      }
      this.tiemid = setInterval(() => {
        this.currentPage++
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1
        }
        this.upData()
      }, 3000)
    },
    // 屏幕大小改变时调用的参数
    screenAdapter() {
      // console.log(this.$refs.ref_seller.offsetWidth)
      const titleFontsize = (this.$refs.ref_seller.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontsize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontsize
            }
          }
        },
        series: [
          {
            barWidth: titleFontsize,
            itemStyle: {
              // 修改柱子圆角
              barBorderRadius: [0, titleFontsize / 2, titleFontsize / 2, 0]
            }
          }
        ]
      }
      this.echartInstance.setOption(adapterOption)
      // 手动的调用图表对象的resize 才能产生效果
      this.echartInstance.resize()
    }
  }
}
</script>

<style lang="scss" scoped></style>
